﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RAD.LightOne;
using System.Data;
using System.Web.Profile;

namespace RAD.LightOne.Web.Security
{

    #region Documentation
    /// <summary>
    /// Classe que encapsula o modelo de dados de Profile.
    /// </summary>
    #endregion
    [ClassName("Class")] //class_name
    [LastPersistence("Register")] //last_persistence
    [DBTable("Profiles")]
    [Serializable]
    public class ProfileBase : MyObject<ProfileBase>, IFull<ProfileBase>
    {
        #region Properties

        [DBField("Username")]
        public string UserName { get; set; }
        [DBField("ApplicationName")]
        public string Application { get; set; }
        [DBField("IsAnonymous")]
        public bool IsAnonymous { get; set; }
        [DBField("LastActivityDate")]
        public DateTime LastActivityDate { get; set; }

        #endregion

        #region Constructors

        public ProfileBase() : base() { }

        #endregion

        #region Methods

        public static ProfileBase FindByUserName(string userName, string applicationName, bool isAuthenticated)
        {
            return ProfileBase.FindBySql("select Id, Class from Profiles where Username={0} and ApplicationName = {1} AND IsAnonymous <> {2}", userName, applicationName, isAuthenticated).FirstOrDefault();
        }

        public static ProfileBase FindByUserNameIgnoreAuthentication(string userName, string applicationName)
        {
            return ProfileBase.FindBySql("select Id, Class from Profiles where Username={0} and ApplicationName = {1} ", userName, applicationName).FirstOrDefault();
        }

        public static ProfileBase[] FindInactiveUsers(DateTime userInactiveSinceDate, ProfileAuthenticationOption authenticationOption)
        {
            return ProfileBase.FindBySql("select Id, Class from Profiles where LastActivityDate < {2}", userInactiveSinceDate);
        }

        public new void Save()
        {
            base.Save();
        }

        public new void Save(IDbTransaction trx)
        {
            base.Save(trx);
        }

        public new void Delete()
        {
            base.Delete();
        }

        public new void Delete(IDbTransaction trx)
        {
            base.Delete(trx);
        }

        public static ProfileBase NewProfileModel(string userName, string applicationName, bool isAuthenticated)
        {
            ProfileBase profile = new ProfileBase() { UserName = userName, Application = applicationName, IsAnonymous = !isAuthenticated };

            profile.LastActivityDate = DateTime.Now;

            return profile;
        }

        public Guid GetId(bool isAuthenticated)
        {
            var user = ProfileBase.FindByUserName(this.UserName, this.Application, isAuthenticated);
            return new Guid(user.id);
        }

        public new void Load()
        {
            base.Load();
        }

        public void Load(IDbTransaction trx)
        {
            base.Load(trx);
        }

        #endregion
    }
}
